package service.account;

import dao.account.AccountDao;
import dao.account.AccountDaoImpl;
import dao.balance.BalanceDao;
import dao.balance.BalanceDaoImpl;
import domain.Account;
import util.EncryptUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AccountServiceImpl implements AccountService{
    AccountDao accountDao = new AccountDaoImpl();
    BalanceDao balanceDao = new BalanceDaoImpl();
    /**
     * 添加账目
     * @param info
     * @param out
     * @param come
     * @param time
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/9/23 17:26
     */
    public int addAccount(String info,float out,float come,String time,String username){
        int i = 0;
        try {
            if (out == 0 && come!=0) {
                i = accountDao.addAccountCome(info, come, time, username);
                balanceDao.Come(username, come);
            }
            if(come == 0 && out !=0) {
                i = accountDao.addAccountOut(info, out, time, username);
                balanceDao.Out(username, out);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 获取明细列表
     * @param username
     * @return List<User>
     * @author 夏添
     * @date 2020/9/23 16:03
     */
    public List<Account> getAccountList(String username){
        List<Account> list = new ArrayList<>();
        try {
            list = accountDao.getAccountList(username);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 统计支出
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/9/23 17:26
     */
    public float countOut(String username){
        float count = 0;
        try {
            count = accountDao.countOut(username);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 统计收入
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/9/23 17:26
     */
    public float countCome(String username){
        float count = 0;
        try {
            count = accountDao.countCome(username);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 本月统计支出
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/11/12 17:26
     */
    public float nowCountOut(String username){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM");
        String time = dateFormat.format(date);
        float count = 0;
        try {
            count = accountDao.MonthCountOut(username,time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 本月统计收入
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/11/12 17:26
     */
    public float nowCountCome(String username){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM");
        String time = dateFormat.format(date);
        float count = 0;
        try {
            count = accountDao.MonthCountCome(username,time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 上月统计支出
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/11/12 17:26
     */
    public float lastCountOut(String username){
        String time = EncryptUtil.getLastMonth();
        float count = 0;
        try {
            count = accountDao.MonthCountOut(username,time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 上月统计收入
     * @param username
     * @return int
     * @author 夏添
     * @date 2020/11/12 17:26
     */
    public float lastCountCome(String username){
        String time = EncryptUtil.getLastMonth();
        float count = 0;
        try {
            count = accountDao.MonthCountCome(username,time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
}
